﻿//-----------------------------------------------------------------------
// <copyright file="ShowNewMessagePopupCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Windows.NewMessagePopup
{
    using ChatBox.DomainObjects.Settings;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;
    using ChatBox.UI.MessageDisplay;
    using ChatBox.XamlUtility;

    /// <summary>
    /// Command to show the "new messages" popup window
    /// </summary>
    public class ShowNewMessagePopupCommand : GenericCommandBase<NewMessageData>
    {
        /// <summary>
        /// The source for Ui setttings
        /// </summary>
        private readonly IUISettingsRepository settingsRepository;

        /// <summary>
        /// Where errors are logged
        /// </summary>
        private readonly ILogger logger;

        /// <summary>
        /// Initializes a new instance of the ShowNewMessagePopupCommand class
        /// </summary>
        /// <param name="settingsRepository">The source for Ui setttings</param>
        /// <param name="logger">The error log</param>
        public ShowNewMessagePopupCommand(IUISettingsRepository settingsRepository, ILogger logger)
        {
            this.settingsRepository = settingsRepository;
            this.logger = logger;
        }

        /// <summary>
        /// Show the New Messages popup window
        /// </summary>
        /// <param name="value">the data to show</param>
        public override void ExecuteOnValue(NewMessageData value)
        {
            NewMessagePopupViewModel viewModel = Locator.Resolve<NewMessagePopupViewModel>();
            this.Populate(value, viewModel);

            NewMessagePopupWindow newMessagePopupWindow = new NewMessagePopupWindow();
            newMessagePopupWindow.DataContext = viewModel;
            viewModel.Window = newMessagePopupWindow;
        }

        /// <summary>
        /// Populate the view model from the data
        /// </summary>
        /// <param name="value">the values to put in</param>
        /// <param name="viewModel">the view model</param>        
        private void Populate(NewMessageData value, NewMessagePopupViewModel viewModel)
        {
            UISettingsData uiSettingsData = this.settingsRepository.ReadSettings();

            FormatOptions privateMessageOptions = new FormatOptions(uiSettingsData.NotificationFontSize, DateTimeDisplay.None, false);
            MessageFormatter messageFormatter = new MessageFormatter(value.HyperlinkContext, this.logger);

            viewModel.Document = messageFormatter.GenerateMessagesDocument(value.Messages, privateMessageOptions);

            int messageCount = value.Messages.Count;
            if (messageCount > 1)
            {
                viewModel.TitleText = messageCount + " new messages";
            }
            else
            {
                viewModel.TitleText = "1 new message";
            }
        }
    }
}
